package com.lst.service.impl;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lst.mapper.NewUserMapper;
import com.lst.mapper.UserMapper;
import com.lst.pojo.EasyUIResult;
import com.lst.pojo.User;
import com.lst.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private NewUserMapper userMapper;

    @Autowired
    private UserMapper userMapper2;

    @Override
    public EasyUIResult pageQuery(Integer page, Integer pagesize) {
       /* // 在查询方法调用之前，调用分页插件的静态方法，中间最好不要隔任何代码
        PageHelper.startPage(page,pagesize);
        //List<User> list = userMapper.queryByPage((page - 1) * pagesize, pagesize);
        List<User> list = userMapper.queryUserAll();
        // 初始化pageInfo对象，所有的分页参数都可以在该对象中获取
        PageInfo<User> userPageInfo = new PageInfo<User>(list);

        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setRows(userPageInfo.getList());
        //获取总页数
      // Long total = userMapper.queryTotal();

        easyUIResult.setTotal(userPageInfo.getTotal());
        return easyUIResult;*/

        //使用通用mappper
        PageHelper.startPage(page, pagesize);
        User record = new User();
        List<User> list = userMapper.select(record);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);

        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setRows(userPageInfo.getList());
        easyUIResult.setTotal(userPageInfo.getTotal());
        return  easyUIResult;


    }

    @Override
    public void addUsers(User user1, User user2) throws Exception {
        /*this.userMapper.addUser(user1);
        // 制造异常
        int i = 1 / 0;
        this.userMapper.addUser(user2);*/


    }

    @Override
    public Boolean addUser(User user) {
        /*boolean flag = false;
        try {
            this.userMapper.addUser(user);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;*/

        //通用mapper
        int index = userMapper.insertSelective(user);
        if(index>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(String[] idStr) {
        /*boolean flag = false;
        try {
            if (idStr != null) {
                for (String s : idStr) {
                    this.userMapper.deleteUser(Integer.parseInt(s));
                }
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;*/
        List<Object> ids= Arrays.asList(idStr);
        //通用mapper
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id",ids);
        int index = userMapper.deleteByExample(criteria);
        if(index>0){
            return true;
        }
        return  false;


    }

    @Override
    public boolean editUser(User user) {
        boolean flag = false;
        try {
            if (user != null) {
                this.userMapper2.editUser(user);
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


}
